package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.AttachTypeEnum;
import cn.huiyunche.base.service.enums.DepartRecepitEnum;
import cn.huiyunche.base.service.framework.utils.QiniuUtils;
import cn.huiyunche.base.service.interfaces.CWaybillAddressService;
import cn.huiyunche.base.service.interfaces.CWaybillAttachService;
import cn.huiyunche.base.service.interfaces.CWaybillContactsService;
import cn.huiyunche.base.service.interfaces.CWaybillDepartRecepitService;
import cn.huiyunche.base.service.model.CWaybillAddress;
import cn.huiyunche.base.service.model.CWaybillAttach;
import cn.huiyunche.base.service.model.CWaybillContacts;
import cn.huiyunche.base.service.vo.detail.DeReDetailVo;
import cn.huiyunche.tools.basic.Collections3;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by LYN on 9/11/16.
 */
@Service
public class CWaybillDepartRecepitServiceImpl implements CWaybillDepartRecepitService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CWaybillDepartRecepitServiceImpl.class);
    @Autowired
    private CWaybillAddressService cWaybillAddressService;

    @Autowired
    private CWaybillContactsService cWaybillContactsService;

    @Autowired
    private CWaybillAttachService cWaybillAttachService;

    /**
     * 获取提车,交车信息
     *
     * @param waybillid
     * @return
     */
    @Override
    public DeReDetailVo getDepartDetail(Long waybillid, int isDepart, int contactType, int attachCategory) {
        LOGGER.error("get depart, recepit info by waybillid: {}, departORrecepit:{}, contactType:{}, attachCategory:{}", isDepart, contactType, attachCategory);
        DeReDetailVo deReDetailVo = new DeReDetailVo();
        //获取提车,交车联系人信息
        CWaybillAddress cWaybillAddress = cWaybillAddressService.getByWaybillid(waybillid);
        if (null != cWaybillAddress) {
            if (isDepart == DepartRecepitEnum.DEPART.getValue()) {
                deReDetailVo.setAddr(cWaybillAddress.getDepartAddr());
                deReDetailVo.setPhone(cWaybillAddress.getDepartPhone());
                deReDetailVo.setContact(cWaybillAddress.getDepartContact());
            } else {
                deReDetailVo.setAddr(cWaybillAddress.getReceiptAddr());
                deReDetailVo.setPhone(cWaybillAddress.getReceiptPhone());
                deReDetailVo.setContact(cWaybillAddress.getReceiptContact());
            }
        }

        //获取提车,交车司机信息
        try {
            List<CWaybillContacts> cWaybillContacts = cWaybillContactsService.selectContacts(waybillid, null);
            if (CollectionUtils.isNotEmpty(cWaybillContacts)) {
                List<CWaybillContacts> contacts = cWaybillContacts.stream().filter(contactx -> contactx.getType() == contactType).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(contacts)) {
                    CWaybillContacts contact = contacts.get(0);
                    deReDetailVo.setDrivername(contact.getContact());
                    deReDetailVo.setDriverphone(contact.getPhone());
                    deReDetailVo.setDrivercardid(contact.getIdno());
                    deReDetailVo.setTracknumber(contact.getLicenseNumner());
                }
            }
        } catch (Exception e) {
            LOGGER.error("get depart, recepit info error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        //获取提车,交车照片
        List<CWaybillAttach> attachList = cWaybillAttachService.getByWaybillid(waybillid, attachCategory, Arrays.asList(AttachTypeEnum.RECEIPT_BILL.getValue(), AttachTypeEnum.RECEIPT_PHOTO.getValue(), AttachTypeEnum.DELIVER_BILL.getValue(), AttachTypeEnum.DELIVER_PHOTO.getValue()));
        if (Collections3.isNotEmpty(attachList)) {
            List<DeReDetailVo.Pic> pics = new ArrayList<>();
            for (CWaybillAttach cWaybillAttach : attachList) {
                DeReDetailVo.Pic pic = deReDetailVo.new Pic();
                pic.setPicurl(QiniuUtils.generateDownloadTicket(cWaybillAttach.getPicKey(), ""));
                pics.add(pic);
            }
            deReDetailVo.setPics(pics);
        }
        return deReDetailVo;
    }
}
